#!/usr/bin/env python

import sys
from os import sep
from os import getcwd

try:
    import pygtk
    pygtk.require('2.0')
except:
    pass
try:
    import gtk
    import gtk.glade
except:
    sys.exit(1)

WELCOME = "GustoPy beta!"


class Gui(object):
    
    def __init__(self):
        
        
        
        self.xml = gtk.glade.XML(getcwd()+sep+'gui'+sep+'gui.glade')
        self.janela = self.xml.get_widget('window1')
        self.frame1 = self.xml.get_widget('frame1')
        self.frame2 = self.xml.get_widget('frame2')
        self.rbCliente = self.xml.get_widget('rbClient')
        self.rbServidor = self.xml.get_widget('rbServer')
        self.entryNome = self.xml.get_widget('entryName')
        self.entryIp = self.xml.get_widget('entryIp')
        self.button = self.xml.get_widget('btnStart')
        self.texto = self.xml.get_widget('txtv')
        self.lbIp = self.xml.get_widget('lbIp')
        
                
        self.texto.set_size_request(600,50)
        #callbacks
        self.button.connect('clicked',self.btnStartCallBack)
        
        self.rbCliente.connect('clicked',self.radioCallBack,'cliente')
        self.rbServidor.connect('clicked',self.radioCallBack,'servidor')
        self.janela.connect('delete_event',gtk.main_quit)
        
        self.janela.set_position(gtk.WIN_POS_CENTER)
        self.janela.set_title('GustoPy')
        self.entryIp.set_sensitive(False)
        self.printTextView(self.texto,WELCOME)
        
        self.janela.show_all()
    
    def printTextView(self,textview,text):
        textview.get_buffer().set_text(text)
    
    
    #CallBacks
    def btnStartCallBack(self,widget):
        self.frame1.destroy() # Destroi o frame antigo
        #Frame2
        self.vboxframe2 = self.xml.get_widget('vbox1f2')
        
        self.vboxframe2.pack_start(self.texto) # add o texto no espaco livre do vbox
        self.texto.show() # mostrar o texto, por algum motivo eh necessario fazer 
        self.janela.add(self.frame2) # add o outro frame na mesma janela
        self.printTextView(self.texto,WELCOME)
         
        
    def radioCallBack(self,widget,data=None):
            if data == 'servidor':
                self.entryIp.set_text('')
                self.entryIp.set_sensitive(False)
            else:
                self.entryIp.set_sensitive(True)
                
            
   
    def main(self):
        gtk.main()
        

if __name__ =="__main__":
    Gui().main()